#include "waitingroom.h"
#include "connection.h"
#include "board.h"

//(*InternalHeaders(WaitingRoom)
#include <wx/bitmap.h>
#include <wx/font.h>
#include <wx/fontenum.h>
#include <wx/fontmap.h>
#include <wx/image.h>
#include <wx/intl.h>
#include <wx/settings.h>
//*)

//(*IdInit(WaitingRoom)
const long WaitingRoom::ID_LISTBOX1 = wxNewId();
const long WaitingRoom::ID_BUTTON2 = wxNewId();
const long WaitingRoom::ID_BUTTON1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(WaitingRoom,wxFrame)
	//(*EventTable(WaitingRoom)
	//*)
END_EVENT_TABLE()

WaitingRoom::WaitingRoom(wxWindow* parent, Connection* server, wxWindowID id)
{
	//(*Initialize(WaitingRoom)
	Create(parent,id,wxEmptyString,wxDefaultPosition,wxDefaultSize,wxDEFAULT_FRAME_STYLE,_T("wxFrame"));
	SetClientSize(wxSize(296,162));
	ListBox1 = new wxListBox(this,ID_LISTBOX1,wxPoint(12,8),wxSize(263,91),0,0,wxLB_SINGLE,wxDefaultValidator,_T("ID_LISTBOX1"));
	Button2 = new wxButton(this,ID_BUTTON2,_("Spectate"),wxPoint(151,106),wxDefaultSize,0,wxDefaultValidator,_T("ID_BUTTON2"));
	Button1 = new wxButton(this,ID_BUTTON1,_("Join"),wxPoint(43,104),wxDefaultSize,0,wxDefaultValidator,_T("ID_BUTTON1"));
	Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&WaitingRoom::OnJoin);
	//*)

	this->server = server;
}

void WaitingRoom::addRoom(Room* r) {
	if(r == NULL) {
	  printf("Rooom not initiallized!\n");
		 return;
	}
    wxString foo(r->name, wxConvLocal);

    // The following code seems to cause crashing.
    ListBox1->InsertItems(1U, &foo, r->ID);
}

WaitingRoom::~WaitingRoom()
{
	delete server;
	//(*Destroy(WaitingRoom)
	//*)
}


void WaitingRoom::OnJoin(wxCommandEvent& event)
{
    int room = ListBox1->GetSelection();
    if(room == wxNOT_FOUND)
        return;
    server->join(room);

    /*
     * I think that we should wait for the server to accept this, but not now
     */

     board* b = new board(NULL, room, server);
     server->addBoard(b);
     b->Show();
}
